<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生数据管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th,
        td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }

        th {
            background-color: #f2f2f2;
        }

        tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        tr:hover {
            background-color: #f1f1f1;
        }

        .form-container {
            margin-bottom: 20px;
        }

        .hidden {
            display: none;
        }
    </style>
</head>

<body>
    <h2>学生数据管理</h2>
    <div class="form-container">
        <h3>新增学生</h3>
        <form id="addStudentForm">
            姓名：<input type="text" id="name" required><br>
            年龄：<input type="number" id="age" required><br>
            专业：<input type="text" id="major" required><br>
            年级：<input type="text" id="grade" required><br>
            <button type="button" onclick="addStudent()">添加学生</button>
        </form>
    </div>
    <div class="form-container hidden" id="editStudentForm">
        <h3>编辑学生</h3>
        <form id="editStudentForm">
            姓名：<input type="text" id="editName" required><br>
            年龄：<input type="number" id="editAge" required><br>
            专业：<input type="text" id="editMajor" required><br>
            年级：<input type="text" id="editGrade" required><br>
            <button type="button" onclick="updateStudent()">更新学生</button>
            <button type="button" onclick="cancelEdit()">取消</button>
        </form>
    </div>
    <table id="studentsTable">
        <thead>
            <tr>
                <th>姓名</th>
                <th>年龄</th>
                <th>专业</th>
                <th>年级</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <!-- 学生数据将通过JavaScript动态插入 -->
        </tbody>
    </table>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            loadStudents();
        });

        function loadStudents() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', 'http://localhost:3000/students', true);
            xhr.onload = function () {
                if (this.status === 200) {
                    var students = JSON.parse(this.responseText);
                    var tableBody = document.getElementById('studentsTable').getElementsByTagName('tbody')[0];
                    tableBody.innerHTML = ''; // 清空表格
                    students.forEach(function (student) {
                        var row = tableBody.insertRow();
                        var cellName = row.insertCell(0);
                        var cellAge = row.insertCell(1);
                        var cellMajor = row.insertCell(2);
                        var cellGrade = row.insertCell(3);
                        var cellActions = row.insertCell(4);

                        cellName.textContent = student.name;
                        cellAge.textContent = student.age;
                        cellMajor.textContent = student.major;
                        cellGrade.textContent = student.grade;

                        var deleteButton = document.createElement('button');
                        deleteButton.textContent = '删除';
                        deleteButton.onclick = function () {
                            deleteStudent(student.id);
                        };
                        cellActions.appendChild(deleteButton);

                        var editButton = document.createElement('button');
                        editButton.textContent = '编辑';
                        editButton.onclick = function () {
                            editStudent(student);
                        };
                        cellActions.appendChild(editButton);
                    });
                } else {
                    console.error('Failed to fetch students data: ' + this.status);
                }
            };
            xhr.onerror = function () {
                console.error('Request error...');
            };
            xhr.send();
        }

        function addStudent() {
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://localhost:3000/students', true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.onload = function () {
                if (this.status === 201) {
                    loadStudents();
                } else {
                    console.error('Failed to add student: ' + this.status);
                }
            };
            xhr.onerror = function () {
                console.error('Request error...');
            };
            var studentData = {
                name: document.getElementById('name').value,
                age: parseInt(document.getElementById('age').value, 10),
                major: document.getElementById('major').value,
                grade: document.getElementById('grade').value
            };
            xhr.send(JSON.stringify(studentData));
        }

        function deleteStudent(id) {
            var xhr = new XMLHttpRequest();
            xhr.open('DELETE', 'http://localhost:3000/students/' + id, true);
            xhr.onload = function () {
                if (this.status === 200) {
                    loadStudents();
                } else {
                    console.error('Failed to delete student: ' + this.status);
                }
            };
            xhr.onerror = function () {
                console.error('Request error...');
            };
            xhr.send();
        }

        function editStudent(student) {
            document.getElementById('editName').value = student.name;
            document.getElementById('editAge').value = student.age;
            document.getElementById('editMajor').value = student.major;
            document.getElementById('editGrade').value = student.grade;
            document.getElementById('editName').setAttribute('data-id', student.id); // 设置 data-id 属性
            document.getElementById('editStudentForm').classList.remove('hidden');
            document.getElementById('addStudentForm').classList.add('hidden');
        }

        function updateStudent() {
            var studentData = {
                id: document.getElementById('editName').getAttribute('data-id'), // 获取 data-id 属性
                name: document.getElementById('editName').value,
                age: parseInt(document.getElementById('editAge').value, 10),
                major: document.getElementById('editMajor').value,
                grade: document.getElementById('editGrade').value
            };
            var xhr = new XMLHttpRequest();
            xhr.open('PUT', 'http://localhost:3000/students/' + studentData.id, true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.onload = function () {
                if (this.status === 200) {
                    loadStudents();
                    cancelEdit();
                } else {
                    console.error('Failed to update student: ' + this.status);
                }
            };
            xhr.onerror = function () {
                console.error('Request error...');
            };
            xhr.send(JSON.stringify(studentData));
        }

        function cancelEdit() {
            document.getElementById('editStudentForm').classList.add('hidden');
            document.getElementById('addStudentForm').classList.remove('hidden');
            document.getElementById('editName').value = '';
            document.getElementById('editAge').value = '';
            document.getElementById('editMajor').value = '';
            document.getElementById('editGrade').value = '';
        }
    </script>
</body>

</html>